<?php
/**
 * wondershare 产品评论活动页面接口文件
 * @auth huangcb
 * */

//note 接口合法性验证
define('INTERFACE_CODE', md5('ws-reviews-nene'));
if( $_GET['code'] != INTERFACE_CODE ) exit('error code!');

define('IN_WONDERSHARE', true);
define('CACHE_DIR', '../../image2/cache/');//缓存文件夹

require_once '../../conf/conf.php';
require_once '../../includes/cls_mysql.php';
require_once 'function.php';

$db = new cls_mysql(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);


//note 取得某个产品的总评论数和平均数
function get_review_num( $db ){
	$pid = get('pid');
	$sql = "SELECT star_1,star_2,star_3,star_4,star_5,(star_1+star_2+star_3+star_4+star_5) AS all_num 
			FROM product_reviews 
			WHERE pid = {$pid}";
	$s = $db->getOneRs($sql);
	
	$star_avg = 0;
	if( $s['all_num'] ) {
		$star_avg = ($s['star_1'] + 2*$s['star_2'] + 3*$s['star_3'] + 4*$s['star_4'] + 5*$s['star_5']) / $s['all_num'];
	}

	$sql = "SELECT url FROM Table_250 WHERE Field_22315 = '{$pid}'";
	$url = $db->getOne( $sql );
	$url = str_replace(array('index.html','/applemacvideo.com/httpdocs/'), array('','http://www.applemacvideo.com/'), $url);

	echo $s['all_num'].','.$star_avg.','.$s['star_1'].','.$s['star_2'].','.$s['star_3'].','.$s['star_4'].','.$s['star_5'].','.$url;
}

//note 支持与反对
function s_o( $db ){
	$did = post('did');
	$type = post('type');
	$ip = post('ip');
	
	$sql = "SELECT COUNT(*) num FROM product_reviews_ip WHERE did = {$did} AND ip = '{$ip}'";
	$num = $db->getOne( $sql );
	
	//有支持或反对过
	if( $num ){
		echo 0;
		exit;
	}
	
	$sql = "INSERT INTO product_reviews_ip SET did = {$did}, ip = '{$ip}'";
	$db->query( $sql );
	
	$so = $type == 1 ? 'support' : 'oppose';
	
	$sql = "UPDATE product_reviews_detail SET {$so} = {$so} + 1 WHERE did = {$did}";
	$ret = $db->query( $sql );
	echo $ret ? 1 : 0;
}


//note 添加评论
function add( $db ){
	$pid = post('pid');
	
	$sql = "INSERT INTO product_reviews_detail(pid,web_type,star,title,review,username,email,ip) 
			VALUE({$pid },{$_POST['web_type']},{$_POST['star']},'{$_POST['title']}','{$_POST['review']}','{$_POST['username']}','{$_POST['email']}','{$_POST['ip']}')";
	$ret = $db->query( $sql );
	
	echo 1;
}

//note 取得产品信息
function get_info( $db ){
	$pid = post('pid');
	$web_type = post('web_type');
	
	$data = array();
	
	$sql = "SELECT p.product_box_middle, t.url FROM product AS p
		LEFT JOIN won_product AS won_p ON p.product_id = won_p.won_product_id 
		LEFT JOIN Table_26 AS t ON won_p.id = t.Field_20044
		WHERE won_p.id = '{$pid}' AND won_p.won_product_delete = 0";
	$data = $db->getOneRs( $sql );
	if( empty( $data['url']) ){
		$sql = "SELECT p.product_box_middle, t.url FROM product AS p
			LEFT JOIN won_product AS won_p ON p.product_id = won_p.won_product_id 
			LEFT JOIN Table_45 AS t ON won_p.id = t.Field_20350
			WHERE won_p.id = '{$pid}' AND won_p.won_product_delete = 0";
		$data = $db->getOneRs( $sql );
	}
	$data['url'] = str_replace( '/applemacvideo.com/httpdocs', 'http://www.applemacvideo.com', $data['url'] );	
	$data['store_url'] = 'http://www.applemacvideo.com/products.html';


	echo json_encode( $data );
}


//---------------------------------------------------------------//
$m = $_GET['m'];
$m( $db );

